import requests
from lxml import etree

main_url = 'http://www.xs.la/17_17786'
res = requests.get(main_url)
context = res.text
selector = etree.HTML(context)
all_title = selector.xpath('//*[@id="list"]/dl/dd/a/text()')
all_link = selector.xpath('//*[@id="list"]/dl/dd/a/@href')

link_list = []  # 正文url列表
# 获取正文url列表
for link in all_link[4:]:
    page_url = main_url + '/' + str(link).split('/')[-1]
    link_list.append(page_url)


for i in range(len(link_list)):
    con_res = requests.get(link_list[i])
    con_selector = etree.HTML(con_res.text)
    all_con = con_selector.xpath('//*[@id="content"]/text()')
    with open('D:/茅山捉鬼人.txt', 'a', encoding='utf8')as file_write:
        file_write.write(all_title[i+4])
        print('{}=======>标题写入'.format(all_title[i+4]))
        for p in range(len(all_con)):
            one_page = ''.join(all_con[p])
            file_write.write(one_page + '\n')
        print('===============>正文写入')












